//
//  CHDockingViewController.m
//  CarFile
//
//  Created by 陈浩 on 2017/8/1.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHDockingViewController.h"
#import "CHJGSXViewController.h"
#import "CHComplainCountCell.h"
#import "CHDockingDetailController.h"
#import "DJ_Model.h"
#import "CHLeftCell.h"
#import "CHRightCell.h"
@interface CHDockingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) UITableView *leftTableView;
@property (nonatomic ,strong) UITableView *rightTableView;
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,copy) NSString *areaid;
@property (nonatomic ,copy) NSString *startTime;
@property (nonatomic ,copy) NSString *endTime;
@property (nonatomic ,copy) NSString *levels;
@property (nonatomic ,strong) UIView *headView;
@property (nonatomic ,assign) NSInteger isDocking;
@end

@implementation CHDockingViewController

-(NSMutableArray *)datas{
    if (!_datas) {
        _datas =[NSMutableArray array];
    }
    return _datas;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //    self.title = @"企业对接情况";
    self.view.backgroundColor = KRGB(239, 239, 249);
    
    self.startTime = [Util getLastDayTime];
    self.endTime = [Util getLastDayTime];
    self.areaid = (NSString *)[Util getValue:@"areaid"];
//    创建导航栏右边按钮
    [self setupRightBarButtonItem];
//    创建主视图
    [self setupUI];
    
    [BQActivityView showActiviTy];
    
//    [super showLoading:YES];
//    请求网络数据
    [self getData];
    
}

-(void)setupRightBarButtonItem{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.bounds = CGRectMake(0, 0, 40 ,15);
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:@"筛选" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(screeningData) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
}


//   筛选
-(void)screeningData{
    
    id obj = [LongriseRouter openURL:@"CHJGSXViewController"];
    if(obj && [obj isKindOfClass:[UIViewController class]])
    {
        UIViewController *vc = obj;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    //判断该类是否可以传参数
    if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
    {
        id<LongriseRouterDelegater> delegater = obj;
        if(delegater)
        {
            
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            params[@"starttime"] = [Util getLastDayTime];
            params[@"endtime"] = [Util getLastDayTime];
           __weak typeof(self) weakSelf = self;
            //进行传值，传入统一的block以供B进行回调
            [delegater setParams:params block:^(id obj) {
                if(obj && [obj isKindOfClass:[NSMutableDictionary class]])
                {
                    NSMutableDictionary *dcit = obj;
                    weakSelf.areaid = dcit[@"areaid"];
                    weakSelf.levels = dcit[@"levels"];
                    weakSelf.startTime = dcit[@"startTime"];
                    weakSelf.endTime = dcit[@"endTime"];
                    [weakSelf getData];
                }
                
            }];
        }
    }

}



-(void)setupUI{
    
    UILabel *classLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
    classLabel.numberOfLines = 0;
    classLabel.text = @"区域";
    classLabel.font = [UIFont systemFontOfSize:14];
    classLabel.textAlignment = NSTextAlignmentCenter;
    classLabel.backgroundColor = KRGB(249, 249, 249);
    classLabel.textColor =  [UIColor colorWithHexString:@"148EC7" alpha:1.0f];
    classLabel.layer.borderColor = [UIColor colorWithHexString:@"999999" alpha:0.1f].CGColor;
    classLabel.layer.borderWidth = 0.5;
    [self.view addSubview:classLabel];
    
    NSArray *titles = [NSArray array];
    
    
    if (_isDocking == 1) {
        titles = @[@"企业数量",@"已注册",@"已对接",@"对接率",@"上传率"];
    }else{
        titles = @[@"企业数量",@"已注册",@"未对接",@"未对接率"];
    }
   
    self.leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 80, UIScreenH - 40 - 64 )];
    self.leftTableView.separatorInset = UIEdgeInsetsZero;
    self.leftTableView.dataSource = self;
    self.leftTableView.delegate= self;
    self.leftTableView.showsVerticalScrollIndicator = NO;
    self.leftTableView.backgroundColor = [UIColor whiteColor];
    self.leftTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.leftTableView];
    
    
    self.rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 80 * titles.count, UIScreenH -40-64)];
    self.rightTableView.separatorInset = UIEdgeInsetsZero;
    self.rightTableView.dataSource = self;
    self.rightTableView.delegate= self;
    self.rightTableView.showsVerticalScrollIndicator = NO;
    self.rightTableView.showsHorizontalScrollIndicator = NO;
    self.rightTableView.backgroundColor = [UIColor whiteColor];
    self.rightTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(80, 0, UIScreenW - 80, UIScreenH - 64)];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    
    
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80 * titles.count, 40)];
    headView.backgroundColor = KRGB(249, 249, 249);
    self.headView = headView;
    for (int i = 0; i< titles.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i *(80 + 0.5), 0, 80- 0.5, 40)];
        label.textColor =  [UIColor colorWithHexString:@"148EC7" alpha:1.0f];
        label.text = titles[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        [self.headView addSubview:label];
    }
    [scrollView addSubview:self.headView];
    [scrollView addSubview:self.rightTableView];
    scrollView.contentSize = CGSizeMake(80 *titles.count, 0);
    [self.view addSubview:scrollView];
    
 
    
}


-(void)getData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"areaid"] = self.areaid;
    params[@"sign"] = [DESCript encrypt:self.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"start"] = self.startTime;
    params[@"end"] = self.endTime;
    params[@"levels"] = self.levels;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;

    [[Globle getInstance].service requestWithServiceName:@"lcipappgovdocklistbyarea" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
 
        [BQActivityView hideActiviTy];
        [self.datas removeAllObjects];
        
        NSLog(@"对接情况%@",result);
        if ([result[@"code"] isEqualToString:@"1"]) {
            
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                
                DJ_Model *model = [DJ_Model objectWithKeyValues:dict];
            
                [self.datas addObject:model];
                
            }
        }
        [self.leftTableView reloadData];
        [self.rightTableView reloadData];
        [super showLoading:NO];
        
    }];
}






//同步左侧右侧
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.rightTableView) {
        
        CGFloat offsetY = self.rightTableView.contentOffset.y;
        
        CGPoint timeOffsetY = self.leftTableView.contentOffset;
        
        timeOffsetY.y = offsetY;
        
        self.leftTableView.contentOffset = timeOffsetY;
        
        if(offsetY == 0) {
            
            self.leftTableView.contentOffset=CGPointZero;
        }
        
    }else{
        
        CGFloat offsetY = self.leftTableView.contentOffset.y;
        
        CGPoint timeOffsetY = self.rightTableView.contentOffset;
        
        timeOffsetY.y = offsetY;
        
        self.rightTableView.contentOffset = timeOffsetY;
        
        if(offsetY == 0) {
            
            self.rightTableView.contentOffset=CGPointZero;
        }
    }
}






#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *userType = (NSString *)[Util getValue:@"usertype"];
    
    if (tableView == self.leftTableView) {
        CHLeftCell *cell = [CHLeftCell cellForTableView:tableView];
        if ([userType isEqualToString:@"99"]) {
           cell.userInteractionEnabled = NO;
        }
        cell.model = self.datas[indexPath.row];
        return cell;
    }else{
        CHRightCell *cell = [CHRightCell cellForTableView:tableView];
        if ([userType isEqualToString:@"99"]) {
            cell.userInteractionEnabled = NO;
        }
        cell.index = self.index;
        cell.isDocking = _isDocking == 1 ? YES :NO;
        cell.model = self.datas[indexPath.row];
        return cell;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (_isDocking == 1) {
        
        id obj = [LongriseRouter openURL:@"CHDockingDetailController"];
        if(obj && [obj isKindOfClass:[UIViewController class]])
        {
            UIViewController *vc = obj;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        //判断该类是否可以传参数
        if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
        {
            id<LongriseRouterDelegater> delegater = obj;
            if(delegater)
            {
                
                NSMutableDictionary *params = [NSMutableDictionary dictionary];
                params[@"model"] = self.datas[indexPath.row];
                params[@"levels"] = self.levels;
                //进行传值，传入统一的block以供B进行回调
                [delegater setParams:params block:^(id obj) {
                    
                    
                }];
            }
        }
    }else{
        
        id obj = [LongriseRouter openURL:@"CHNoDockingDetailController"];
        if(obj && [obj isKindOfClass:[UIViewController class]])
        {
            UIViewController *vc = obj;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        //判断该类是否可以传参数
        if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
        {
            id<LongriseRouterDelegater> delegater = obj;
            if(delegater)
            {
                
                NSMutableDictionary *params = [NSMutableDictionary dictionary];
                params[@"model"] = self.datas[indexPath.row];
                params[@"levels"] = self.levels;
                //进行传值，传入统一的block以供B进行回调
                [delegater setParams:params block:^(id obj) {
                    
                    
                }];
            }
        }
        
    }
    
  
}




/**
 *  显示完全分割线
 */
-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


#pragma mark LongriseRouterDelegater代理回调方法
-(void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    if (obj && [obj isKindOfClass:[NSMutableDictionary class]]) {
        
        NSMutableDictionary *dict = obj;
        self.title = dict[@"title"];
        self.index = [dict[@"index"] integerValue];
        _isDocking = [dict[@"docking"] integerValue];
        
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
